4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
19 using System
.Collections
;
20 using System
.Reflection
.Emit
;
22 internal sealed class CustomAttributeList
: AST
{
23 private ArrayList list
;
24 private ArrayList customAttributes
;
25 private bool alreadyPartiallyEvaluated
;
27 internal CustomAttributeList(Context context
)
29 this.list
= new ArrayList();
30 this.customAttributes
= null;
31 this.alreadyPartiallyEvaluated
= false;
34 internal void Append(CustomAttribute elem
){
36 this.context
.UpdateWith(elem
.context
);
39 internal bool ContainsExpandoAttribute(){ //Use only before partial evaluation has been done
40 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
41 CustomAttribute ca
= (CustomAttribute
)this.list
[i
];
42 if (ca
== null) continue;
43 if (ca
.IsExpandoAttribute()) return true;
48 // Assume the custom attribute list has been partially evaluated already
49 internal CustomAttribute
GetAttribute(Type attributeClass
){
50 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
51 CustomAttribute ca
= (CustomAttribute
)this.list
[i
];
52 if (ca
== null) continue;
53 Object type
= ca
.type
;
55 if (type
== attributeClass
)
56 return (CustomAttribute
)this.list
[i
];
61 internal override Object
Evaluate(){
62 return this.Evaluate(false);
65 internal Object
Evaluate(bool getForProperty
){
66 int n
= this.list
.Count
;
67 ArrayList attrValues
= new ArrayList(n
);
68 for (int i
= 0; i
< n
; i
++){
69 CustomAttribute ca
= (CustomAttribute
)this.list
[i
];
70 if (ca
== null) continue;
71 if (ca
.raiseToPropertyLevel
){
72 if (!getForProperty
) continue;
74 if (getForProperty
) continue;
75 attrValues
.Add(ca
.Evaluate());
77 Object
[] result
= new Object
[attrValues
.Count
];
78 attrValues
.CopyTo(result
);
82 internal CustomAttributeBuilder
[] GetCustomAttributeBuilders(bool getForProperty
){
83 this.customAttributes
= new ArrayList(this.list
.Count
);
84 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
85 CustomAttribute ca
= (CustomAttribute
)this.list
[i
];
86 if (ca
== null) continue;
87 if (ca
.raiseToPropertyLevel
){
88 if (!getForProperty
) continue;
90 if (getForProperty
) continue;
91 CustomAttributeBuilder attribute
= ca
.GetCustomAttribute();
92 if (attribute
!= null)
93 this.customAttributes
.Add(attribute
);
95 CustomAttributeBuilder
[] result
= new CustomAttributeBuilder
[this.customAttributes
.Count
];
96 this.customAttributes
.CopyTo(result
);
100 internal override AST
PartiallyEvaluate(){
101 if (this.alreadyPartiallyEvaluated
) return this;
102 this.alreadyPartiallyEvaluated
= true;
103 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++)
104 this.list
[i
] = ((CustomAttribute
)this.list
[i
]).PartiallyEvaluate();
105 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++){
106 CustomAttribute ca
= (CustomAttribute
)this.list
[i
];
107 if (ca
== null) continue; //Already found to be invalid
108 Object caType
= ca
.GetTypeIfAttributeHasToBeUnique();
109 if (caType
== null) continue;
110 for (int j
= i
+1; j
< n
; j
++){
111 CustomAttribute caj
= (CustomAttribute
)this.list
[j
];
112 if (caj
== null) continue;
113 if (caType
== caj
.type
){
114 caj
.context
.HandleError(JSError
.CustomAttributeUsedMoreThanOnce
);
115 this.list
[j
] = null; //Remove duplicate
122 internal void Remove(CustomAttribute elem
){
123 this.list
.Remove(elem
);
126 internal void SetTarget(AST target
){
127 for (int i
= 0, n
= this.list
.Count
; i
< n
; i
++)
128 ((CustomAttribute
)this.list
[i
]).SetTarget(target
);
131 internal override void TranslateToIL(ILGenerator il
, Type rtype
){
134 internal override void TranslateToILInitializer(ILGenerator il
){